<?php

include_once('config/dbconf.php');
include_once('db/mysql.php');
//include_once('db/select_builder.php');

$content = $_REQUEST['content'];
$mysql = new MySQL($dbconf);

switch ($content) {
	case 'serials': {
		$result = $mysql->sql_execute("SELECT id, caption FROM serials;");
		break;
	}
	case 'episodes': {
		$ids = $_REQUEST['ids'];
		if ($ids == 'all') {
			$result = $mysql->sql_execute("SELECT episodes.id, release_date, number, serials.id AS serial_id, serials.caption AS serial, released FROM episodes, serials WHERE serial_id = serials.id;");
		} else {
			$result = $mysql->sql_execute("SELECT episodes.id, release_date, number, serials.id AS serial_id, serials.caption AS serial, released FROM episodes, serials WHERE serial_id = serials.id AND serial_id IN $ids;");
		}
		break;
	}
}

$out = json_encode(mysql_fetch_assoc($result));
while ( $row = mysql_fetch_assoc($result)) {
	$out .= "," . json_encode($row);
}

echo $_REQUEST['jsoncallback'], "([", $out, "]);";

?>